<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 公开不授权控制器
//-------------------------

namespace app\domin\controller;

use app\domin\Base;
use think\Config;
use think\Db;
use think\Loader;
use think\exception\HttpException;

use com\gdl\newagk\dataobject\adminlogin\AdminloginRequestDTO;
use com\gdl\newagk\interfaces\AdminloginServiceClient;

use com\gdl\newagk\interfaces\AuthorityServiceClient;
use com\gdl\newagk\dataobject\authority\AuthorityRequestDTO; 

use com\gdl\newagk\interfaces\UnitsServiceClient;
use com\gdl\newagk\dataobject\units\UnitsDTO;
use com\gdl\newagk\dataobject\units\UnitsRequestDTO;


class Authorityc extends Base
{
	
	//查询组织结构
	public function lookeOver()
    {
		$unitid = $this->request->param('unitId') ? $this->request->param('unitId/d') : 0;//部门id  现在是用户传 改完后直接查询管理员部门完成
		$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$title = $this->request->param('title') ? getStr($this->request->param('title')) : '';

			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new UnitsDTO();
				$engprojectDo->unitId	= $unitid;
				$engprojectDo->userId	= $adminuserid;
				$engprojectDo->adminReg		= $adminReg;
				$engprojectDo->filename['title']	= urldecode($title);
				$result = $api->lookeOver($engprojectDo);

				$plaform = $result->datas[0];
				$plaform['children']=[];
				if($result->code){
					/*****测试原型*****/
					if(empty( $result->data)){
						$data =  ['msg'=>$result->message,'code'=>'200','data'=>[]];
					} else {
					$nodes = $result->data;
					$datas = list_to_tree($nodes);
					$plaform['children'] = $datas;
					//checkstate 1是选中 0是未选中
					$data =  ['msg'=>'获取成功','code'=>'200','data'=>[$plaform]];
					}
				} else {
					//checkstate 1是选中 0是未选中
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}

				
			} catch (Exception $ex) {
				$data = ['msg'=>'获取失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
		
	}
	
	
	public function lookeUnits()
    {

		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$groupId = $this->request->param('groupId') ? $this->request->param('groupId/d') : 0;
		
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$title = $this->request->param('title') ? getStr($this->request->param('title')) : '';
		if (!$adminUserId ) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new AuthorityRequestDTO();
				$engprojectDo->filename['adminUserId']	= $adminUserId;
				$engprojectDo->filename['adminReg']		= $adminReg;
				$engprojectDo->filename['groupId']		= $groupId;
				$engprojectDo->filename['title']		= $title;
				$result = $api->lookeUnits($engprojectDo);

				if($result->code){
					$list = [];
					foreach($result->data as $v){
						$v['checked'] = $v['checked']==1 ? true : false;
						$list[] = $v;
					}
					$data =  ['msg'=>'获取成功','code'=>'200','data'=>$list];
				} else {
					$data =  ['msg'=>$result->message,'code'=>'200','data'=>[]];
				}

				//checkstate 1是选中 0是未选中
				
			} catch (Exception $ex) {
				$data = ['msg'=>'获取失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
		
	}
	
	
	public function lookeUserUnits()
    {

		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$groupId = $this->request->param('groupId') ? $this->request->param('groupId/d') : 0;
		
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$title = $this->request->param('title') ? getStr($this->request->param('title')) : '';
		$userId = $this->request->param('userId') ? $this->request->param('userId') : 0;
		if (!$userId || !$adminUserId ) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
            }
		
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new AuthorityRequestDTO();
				$engprojectDo->filename['adminUserId']	= $adminUserId;
				$engprojectDo->filename['adminReg']		= $adminReg;
				$engprojectDo->filename['groupId']		= $groupId;
				$engprojectDo->filename['title']		= $title;
				$engprojectDo->filename['userId']		= $userId;
				
				$result = $api->lookeUserUnits($engprojectDo);

				if($result->code){
					$list = [];
					$userUnits = $result->datas[0];
					$unitid = $result->datas[1];
					foreach($result->data as $v){
						$v['checked'] = in_array($v["id"],$userUnits) ? true : false;
						$v['disabled'] = in_array($v["id"],$unitid) ? false : true;
						$v['expand'] = true;
						$list[] = $v;
					}

					$data =  ['msg'=>'获取成功','code'=>'200','data'=>list_to_tree($list)];
				} else {
					$data =  ['msg'=>$result->message,'code'=>'200','data'=>[]];
				}

				//checkstate 1是选中 0是未选中
				
			} catch (Exception $ex) {
				$data = ['msg'=>'获取失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
		
	}
	
	/*
	*添加或修改单位
	*/
	
	public function edit()
    {
		$data = $this->request->post();
		if (!$this->request->isPost() || empty($data['platformId'])) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
			}
			
			
			$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;

			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new UnitsDTO();
				$engprojectDo->field = json_encode($data);
				$engprojectDo->userId	= $adminuserid;
				$result = $api->edit($engprojectDo);

				//checkstate 1是选中 0是未选中
				$data =  ['msg'=>$result->message,'code'=>'200','data'=>[]];
			} catch (Exception $ex) {
				$data = ['msg'=>'操作失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
	}
	
	
	//资源查询
	public function resourcList()
    {
		$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$groupid = $this->request->param('groupId') ? getStr($this->request->param('groupId'),'int') : 0;
		$typesof = $this->request->param('typesOf') ? getStr($this->request->param('typesOf')) : '';
		$title = $this->request->param('title') ? getStr($this->request->param('title'),'string') : '';
		if(!$groupid){
			$data = ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new AuthorityRequestDTO();
				$engprojectDo->filename['adminuserid']	= $adminuserid;
				$engprojectDo->filename['adminReg']			= $adminReg;
				$engprojectDo->filename['groupid']		= $groupid;
				$engprojectDo->filename['typesof']		= $typesof;
				$engprojectDo->filename['title']		= $title;
				$result = $api->resourcList($engprojectDo);

				if($result->code){
					/*****测试原型*****/
					if(empty($result->data)){
						$datas = [];
					} else {
						$datac = $result->data;
						$datas = $result->datas[0];
						$datas[0] = '默认部门';
						$list = [];
						foreach($datac as $v){
							$list[$v['unitId']]['id'] = $v['unitId'];
							$list[$v['unitId']]['title'] = empty($datas[$v['unitId']]) ? $datas[0] : $datas[$v['unitId']] ;
							$v['expand'] = $v['expand']==2 ? false : true;
							$v['title'] = $v['name'];
							$list[$v['unitId']]["children"][] = $v;
						}
					}
					$data =  ['msg'=>$result->message,'code'=>'200','data'=>array_values($list)];
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'获取失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
	}
	
	/*
	*单位删除
	*/
	public function unitDelete()
    {

		$unitid = $this->request->param('unitId') ? $this->request->param('unitId/d') : 0;//部门id 这里代表的是要操作那个部门
		$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId/d') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';

			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new UnitsRequestDTO();
				$engprojectDo->unitId	= $unitid;
				$engprojectDo->userId	= $adminuserid;
				$engprojectDo->adminReg		= $adminReg;
				$engprojectDo->filename['operating'] = 'deletes';
				$result = $api->enabled($engprojectDo);

				if($result->code){
					$data =  ['msg'=>$result->message,'code'=>'200','data'=>[]];
				} else {
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'操作失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
	}
	
	
	/*
	*单位启用禁用
	*/
	public function unitEnabled()
    {

		$unitid = $this->request->param('unitId') ? $this->request->param('unitId/d') : 0;//部门id 这里代表的是要操作那个部门
		$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$status = $this->request->param('status') ? $this->request->param('status/d') : 0;
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new UnitsRequestDTO();
				$engprojectDo->unitId	= $unitid;
				$engprojectDo->userId	= $adminuserid;
				$engprojectDo->adminReg		= $adminReg;
				$engprojectDo->filename['status'] = $status;
				$result = $api->enabled($engprojectDo);

				if($result->code){
					$data =  ['msg'=>$result->message,'code'=>'200','data'=>$result->data[0]['status']==2 ? 1 : 2];
				} else {
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'操作失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
	}
	
	
	/*
	*单位下所有权限组启用禁用
	*/
	public function listEnabled()
    {

		$unitid = $this->request->param('unitId') ? $this->request->param('unitId/d') : 0;//部门id 这里代表的是要操作那个部门
		$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$status = $this->request->param('status') ? $this->request->param('status/d') : 2;
		$groupsid = $this->request->param('groupsId') ? $this->request->param('groupsId/d') : 0;//权限组id
		if(!$unitid){
			$data = ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new UnitsRequestDTO();
				$engprojectDo->unitId	= $unitid;
				$engprojectDo->userId	= $adminuserid;
				$engprojectDo->adminReg		= $adminReg;
				$engprojectDo->filename['status'] = $status;
				$engprojectDo->filename['groupsid'] = $groupsid;
				$engprojectDo->filename['operating'] = 'status';
				$result = $api->listEnabled($engprojectDo);
				if($result->code){
					$data =  ['msg'=>$result->message,'code'=>'200','data'=>$result->data[0]['status']];
				} else {
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'操作失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
	}
	
	/*
	*单位下所有权限组删除
	*/
	public function listDel()
    {


		$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$status = $this->request->param('status') ? $this->request->param('status/d') : 2;
		$groupsid = $this->request->param('groupsId') ? $this->request->param('groupsId/d') : 0;//权限组id
		if(!$groupsid || !$adminuserid){
			$data = ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new UnitsRequestDTO();

				$engprojectDo->userId	= $adminuserid;
				$engprojectDo->adminReg		= $adminReg;
				$engprojectDo->filename['status'] = $status;
				$engprojectDo->filename['groupsid'] = $groupsid;
				$engprojectDo->filename['operating'] = 'del';
				$result = $api->listEnabled($engprojectDo);
				if($result->code){
					$data =  ['msg'=>$result->message,'code'=>'200','data'=>$result->data[0]['status']];
				} else {
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'操作失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);
	}
	
	
	/*
	*查询权限组的权限
	*/
	public function listGroup()
    {
			$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
			$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
			$groupId = $this->request->param('groupId') ? $this->request->param('groupId/d') : 0;//权限组id
			$pid = $this->request->param('pid') ? $this->request->param('pid/d') : 0;//上级id 有这个的时候查询的就是第二级
			$appId = $this->request->param('appId') ? $this->request->param('appId/d') : 0;

				if ( empty($adminReg) || !$adminUserId || !$appId ) {
					$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
					return json_encode($data);
				}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new AuthorityRequestDTO();

				$engprojectDo->filename['adminUserId']	= $adminUserId;
				$engprojectDo->filename['adminReg']	= $adminReg;
				$engprojectDo->filename['groupId']	= $groupId;
				$engprojectDo->filename['pid']	= $pid;
				$engprojectDo->filename['appId']	= $appId;
				$result = $api->accessList($engprojectDo);

				if($result->code){
					if(empty($result->data)){
						$data =  ['msg'=>$result->message,'code'=>'200','data'=>[]];
						return json_encode($data);	
					}
					$datas = [];
					
					foreach($result->data as $v){
						$v['checked'] = $v['checked']==1 ? true : false;
						$v['disabled'] = $v['display'] = $v['display']==1 ? true : false;
						$v['expand'] = true;
						$datas[] = $v;
					}

					$results = list_to_tree($datas, "id", "pid", "children");

					$data =  ['msg'=>$result->message,'code'=>'200','data'=>$results,'rights'=>$result->datas[0]];
				} else {
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}

			} catch (Exception $ex) {
				$data = ['msg'=>'操作失败','code'=>'400','data'=>[]];
			}
			return json_encode($data);	
	}


	public function authorized()
    {
		//$role_id = $this->request->param('id/d');
		$groupId = $this->request->param('groupsId') ? $this->request->param('groupsId/d') : 0;
		$unitsid = $this->request->param('unitsId') ? $this->request->param('unitsId/d') : 0;
		$data =  ['msg'=>'操作异常','code'=>'500','data'=>[]];
		if ($this->request->isPost()) {
			$datas = $this->request->post();
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new AuthorityRequestDTO();
				$engprojectDo->filename = array_merge($datas["node"],$datas["node2"]); //这个记得处理 只能是一维数组
				$engprojectDo->filename['roleId']	= $groupId;
				$engprojectDo->filename['resourc']	=  json_encode($datas["resourc"]);//资源
				$engprojectDo->id = $unitsid;
				$result = $api->authorized($engprojectDo);

				//checkstate 1是选中 0是未选中
				if($result->code){
					$data =  ['msg'=>'成功','code'=>'200','data'=>[]];
				} else {
					$data =  ['msg'=>'失败','code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data =  ['msg'=>'失败','code'=>'500','data'=>[]];
			}
			
		}
		return json_encode($data);
	}
	
	//管理员列表
	public function adminList()
    {
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$title = $this->request->param('title') ? getStr($this->request->param('title')) : '';
		$unitsId = $this->request->param('unitsId') ? getStr($this->request->param('unitsId')) : 0;

		if(!$adminUserId ){
			$data =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new UnitsDTO();

				$engprojectDo->adminReg = $adminReg;
				$engprojectDo->userId	= $adminUserId;
				$engprojectDo->filename['title']	= $title;
				$engprojectDo->filename['unitsId']	= $unitsId;
				$result = $api->adminList($engprojectDo);


				$plaform = [];
				if($result->code){
					if(empty($result->data)){
						$data =  ['msg'=>'成功','code'=>'200','data'=>[]];
					} else {
						$data =  ['msg'=>'成功','code'=>'200','data'=>$result->data];
					}
				} else {
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data =  ['msg'=>'失败','code'=>'500','data'=>[]];
			}
			
		
		return json_encode($data);
	}
	
	
	//添加对应单位管理员
	public function adminEdit()
    {

		$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$appId = $this->request->param('appId') ? intval($this->request->param('appId')) : 0;
		$data = $this->request->post();
	
		$administr = [];
		
		$administr['groupId'] = $administr['groupsId'] = !empty($data['groupsId']) ? intval($data['groupsId']) : 0;
		$administr['mobile'] = !empty($data['mobile']) ? $data['mobile'] : 0;
		$administr['passWord'] = !empty($data['passWord']) ? $data['passWord'] : '';
		$administr['plaformId'] = !empty($data['plaformId']) ? intval($data['plaformId']) : 0;
		$administr['realName'] = !empty($data['realName']) ? $data['realName'] : '';
		$administr['userId'] = !empty($data['userId']) ? intval($data['userId']) : 0;
		$administr['userName'] = !empty($data['userName']) ? $data['userName'] : '';
		$administr['unitsId'] = !empty($data['unitsId']) ? intval($data['unitsId']) : 0;
		$data['userId'] = !empty($data['userId']) ? intval($data['userId']) : 0;
		$node =  !empty($data['node']) ? explode(',',$data['node']) : [];
		$resourc =  !empty($data['resourc']) ? explode(',',$data['resourc']) : [];
		

		if(!$adminuserid || empty($administr['groupsId'])){
			$data =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
		
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				$autApi = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new UnitsDTO();

				$engprojectDo->adminReg = $adminReg;
				$engprojectDo->userId	= $adminuserid;
				if ($this->request->isPost()) {
					if(empty($administr)){
						$data =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
						return json_encode($data);
					}
					$engprojectDo->filename = $administr;
					$result = $api->adminEdit($engprojectDo);

					if($result->code){
						$userid = $result->data[0]['userid'];
						\ApiClient::build($autApi);
						$authorit = new AuthorityRequestDTO();
						$resourc = empty($resourc) ? [$administr['unitsId']] : array_merge($resourc,[$administr['unitsId']]);
						$authorit->filename['node'] = json_encode($node);
						$authorit->filename['adminUserId']	= $userid;
						$authorit->filename['groupId']	= $administr['groupsId'];
						$authorit->filename['resourc']	= json_encode($resourc);
						$authorit->filename['appId']	= $appId;

						$results = $autApi->userAuthorized($authorit);

						if($results->code){
							$data =  ['msg'=>$results->message,'code'=>'200','data'=>[]];
						} else {
							$data =  ['msg'=>$results->message,'code'=>'400','data'=>[]];
						}
						
					} else {
						$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
					}
				} else {
					$data =  ['msg'=>'未知方式','code'=>'500','data'=>[]];
				}
			} catch (Exception $ex) {
				$data =  ['msg'=>'失败','code'=>'500','data'=>[]];
			}
			return json_encode($data);
	}
	
	
	public function adminUnits()
    {

		$adminuserid = $this->request->post('adminUserId') ? $this->request->post('adminUserId') : 0;

		$appId = $this->request->post('appId') ? intval($this->request->post('appId')) : 1;
		$userId = $this->request->post('userId') ? intval($this->request->post('userId')) : 0;
		$resourc =  $this->request->post('resourc') ? explode(',',$this->request->post('resourc')) : [];
		

		if(!$adminuserid || !$userId){
			$data =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
		
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$autApi = new AuthorityServiceClient(NULL);
				\ApiClient::build($autApi);
				$authorit = new AuthorityRequestDTO();
				$authorit->filename['adminUserId']	= $userId;
				$authorit->filename['resourc']	= json_encode($resourc);
				$authorit->filename['appId']	= $appId;
				$results = $autApi->userAuthorized($authorit);
				if($results->code){
					$data =  ['msg'=>$results->message,'code'=>'200','data'=>[]];
				} else {
					$data =  ['msg'=>$results->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data =  ['msg'=>'失败','code'=>'500','data'=>[]];
			}
			return json_encode($data);
	}
	
	
	//获取对应单位管理员信息
	public function adminLook()
    {

		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId/d') : 0;


		if(!$adminUserId){
			$data =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new UnitsDTO();

				$engprojectDo->userId	= $adminUserId;

				$result = $api->adminLook($engprojectDo);

				if($result->code){
				
					if(empty($result->data)){
						$data =  ['msg'=>$result->message,'code'=>'200','data'=>[]];
					} else {
						$data =  ['msg'=>$result->message,'code'=>'200','data'=>$result->data];
					}
				} else {
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
					
			} catch (Exception $ex) {
				$data =  ['msg'=>'失败','code'=>'500','data'=>[]];
			}
			return json_encode($data);
	}
	
	//查看个人权限
	public function userauthorized()
    {

		$userId = $this->request->param('userId') ? $this->request->param('userId/d') : 0; 

		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId/d') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$appId = $this->request->param('appId') ? $this->request->param('appId/d') : 0;
            if (!$userId  || !$appId || !$adminUserId ) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
            }
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new AuthorityRequestDTO();

				$engprojectDo->filename['userId']	= $userId;
				$engprojectDo->filename['adminUserId']	= $adminUserId;
				$engprojectDo->filename['adminReg']	= $adminReg;
				$engprojectDo->filename['appId']	= $appId;
				$result = $api->accessuserList($engprojectDo);

				if($result->code){
					$datas = [];
					foreach( $result->data as $v){
						$v['hasChildren'] = $v['hasChildren']==1 ? true : false;
						$datas[] = $v;
					}
					$results = list_to_tree($datas, "id", "pid", "children");
					$data = ['msg'=>$result->message,'code'=>'200','data'=>$results];
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'异常','code'=>'500','data'=>[]];
			}
			

				return json_encode($data);
		
	}
	
	
	//获取权限组列表【新版】
	public function autList()
    {
		$appId = $this->request->param('appId') ? $this->request->param('appId/d') : 0;
		$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId/d') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$title = $this->request->param('title') ? getStr($this->request->param('title')) : '';
            if (!$adminuserid || !$appId ) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
            }
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new AuthorityRequestDTO();
				$engprojectDo->filename['adminuserid']	= $adminuserid;
				$engprojectDo->filename['adminReg']	= $adminReg;
				$engprojectDo->filename['appId']	= $appId;
				$engprojectDo->filename['title']	= $title;
				$result = $api->autList($engprojectDo);

				if($result->code){
					if(empty($result->data)){
						$data = ['msg'=>$result->message,'code'=>'200','data'=>[]];
					} else {
						$data = ['msg'=>$result->message,'code'=>'200','data'=>$result->data];
					}
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'异常','code'=>'500','data'=>[]];
			}
			

				return json_encode($data);
		
	}
	
	//添加权限组
	public function groupsAdd()
    {
		
		$data = $this->request->post();
		
		$adminUserId = $this->request->post('adminUserId') ?$this->request->post('adminUserId/d') : 0;
		$adminReg =  $this->request->post('adminReg') ? getStr($this->request->post('adminReg'),'string') : '';
		$title = $this->request->post('title') ? getStr($this->request->post('title'),'string') : '';
		$appId = $this->request->post('appId') ?$this->request->post('appId/d') : 0;
		$status = $this->request->post('status') ?$this->request->post('status/d') : 1;
		$groupId = $this->request->post('groupId') ? $this->request->post('groupId/d') : 0;
		$node =  !empty($data['node']) ? explode(',',$data['node']) : [];
		$resourc = !empty($data['resourc']) ? explode(',',$data['resourc']): [];
		
		/*
		$adminUserId = $this->request->param('adminUserId') ?$this->request->param('adminUserId/d') : 0;
		$adminReg =  $this->request->param('adminReg') ? getStr($this->request->param('adminReg'),'string') : '';
		$title = $this->request->param('title') ? getStr($this->request->param('title'),'string') : '';
		$appId = $this->request->param('appId') ?$this->request->param('appId/d') : 0;
		$status = $this->request->param('status') ?$this->request->param('status/d') : 1;
		$groupId = $this->request->param('groupId') ? $this->request->param('groupId/d') : 0;
		$node =  [11,8,38,32];
		$resourc =  [];
*/
		if(!$appId || !$adminUserId || $title==''){
			$data =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				
				\ApiClient::build($api);
				$autDo = $engprojectDo = new AuthorityRequestDTO();

				$engprojectDo->filename['adminUserId']	= $adminUserId;
				$engprojectDo->filename['adminReg']	= $adminReg;
				$engprojectDo->filename['title']	= $title;
				$engprojectDo->filename['groupId']	= $groupId;
				$engprojectDo->filename['appId']	= $appId;
				$engprojectDo->filename['status']	= $status;
				$result = $api->groupsAdd($engprojectDo);

				if($result->code){
					$autDo->filename['adminUserId']	= $adminUserId;
					$autDo->filename['appId']	= $appId;
					$autDo->filename['node']	= json_encode($node);//这个记得处理 只能是一维数组
					$autDo->filename['groupId']	= $result->datas[0]['id'];
					$autDo->filename['resourc']	=  json_encode($resourc);//资源

					$results = $api->authorized($autDo);
		
					//checkstate 1是选中 0是未选中
					if($results->code){
						$data =  ['msg'=>'成功','code'=>'200','data'=>[]];
					} else {
						$data =  ['msg'=>'失败','code'=>'400','data'=>[]];
					}

					} else {
						$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
					}
				 
			} catch (Exception $ex) {
				$data =  ['msg'=>'失败','code'=>'500','data'=>[]];
			}
			return json_encode($data);
	}
	
	
	//查看权限组详情
	public function groupsLook()
    {


		$adminUserId = $this->request->param('adminUserId') ?$this->request->param('adminUserId/d') : 0;
		$adminReg =  $this->request->param('adminReg') ? getStr($this->request->param('adminReg'),'string') : '';
		$groupId = $this->request->param('groupId') ?$this->request->param('groupId/d') : 0;
		if(!$adminUserId || !$groupId){
			$data =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				
				\ApiClient::build($api);
				$autDo = $engprojectDo = new AuthorityRequestDTO();

				$engprojectDo->filename['adminUserId']	= $adminUserId;
				$engprojectDo->filename['adminReg']	= $adminReg;
				$engprojectDo->filename['groupId']	= $groupId;
	
				$result = $api->groupsLook($engprojectDo);

				if($result->code){
					$data = empty($result->data) ? [] : $result->data[0];
						$data =  ['msg'=>$result->message,'code'=>'200','data'=>$data];
					} else {
						$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
					}
				 
			} catch (Exception $ex) {
				$data =  ['msg'=>'失败','code'=>'500','data'=>[]];
			}
			return json_encode($data);
	}
	
	//获取管理员能操作的所有单位
	public function managements()
    {

		$unitsid = $this->request->param('unitId') ? $this->request->param('unitId/d') : 0;
		$adminuserid = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		if(!$unitsid){
			$data =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new UnitsServiceClient(NULL);
				\ApiClient::build($api);

				$engprojectDo = new UnitsDTO();
				$engprojectDo->unitId = $unitsid;
				$engprojectDo->adminReg = $adminReg;
				$engprojectDo->userId	= $adminuserid;
				$result = $api->managements($engprojectDo);
				if($result->code){

					$data =  ['msg'=>'成功','code'=>'200','data'=>$result->data];
				} else {
					$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data =  ['msg'=>'失败','code'=>'500','data'=>[]];
			}
			
		
		return json_encode($data);
	}
	
	
	
	//直接给管理员个人授权
	public function userauthor()
    {
		$unitsid = $this->request->param('unitId') ? $this->request->param('unitId/d') : 0;
		$userid = $this->request->param('userId') ? $this->request->param('userId/d') : 0;
		$node =  $this->request->param('node') ? getStr($this->request->param('node')) : [];
		$node2 =  $this->request->param('node2') ? getStr($this->request->param('node2')) : [];
		$roleId = $this->request->param('groupsId') ? $this->request->param('groupsId/d') : 0;
		$resourc =  $this->request->param('resourc') ? getStr($this->request->param('resourc')) : [];
		if(!$unitsid || !$roleId){
			$data =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}
			if ($this->request->isPost()) {
				$datas = $this->request->post();
				try {
					\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
					$api = new AuthorityServiceClient(NULL);
					\ApiClient::build($api);
						$authorit = new AuthorityRequestDTO();
						
						$authorit->filename = array_merge($node,$node2);
						$authorit->filename['unitid']	= $unitsid;
						$authorit->filename['userid']	= $userid;
						$authorit->filename['role_id']	= $roleId;
						$authorit->filename['resourc']	= json_encode($resourc);
						$results = $api->userAuthorized($engprojectDo);

					//checkstate 1是选中 0是未选中
					if($result->code){
							$data =  ['msg'=>$result->message,'code'=>'200','data'=>[]];
					} else {
							$data =  ['msg'=>$result->message,'code'=>'400','data'=>[]];
					}
				} catch (Exception $ex) {
						$data =  ['msg'=>'操作失败','code'=>'400','data'=>[]];
				}
				return json_encode($data);
			
			}
			$data =  ['msg'=>'提交格式错误','code'=>'500','data'=>[]];
			return json_encode($data);
		}
	
		public function navMenu()
    {

		$unitId = $this->request->param('unitId') ? $this->request->param('unitId/d') : 0;
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId/d') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$appId = $this->request->param('appId') ? $this->request->param('appId/d') : 0;
            if (!$adminUserId ) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
            }
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new AuthorityRequestDTO();
				$engprojectDo->filename['adminUserId']	= $adminUserId;
				$engprojectDo->filename['adminReg']	= $adminReg;
				$engprojectDo->filename['unitId']	= $unitId;
				$engprojectDo->filename['appId']	= $appId;

				$result = $api->navigationMenu($engprojectDo);
			

				if($result->code){
					$results = list_to_tree($result->data, "id", "pid", "children");

					$data = ['msg'=>$result->message,'code'=>'200','data'=>$results];
				} else {
					$data = ['msg'=>$result->message,'code'=>'200','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'异常','code'=>'500','data'=>[]];
			}
				return json_encode($data);
		}
		
			//菜单栏
	public function navList()
    {

		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId/d') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$unitId = $this->request->param('unitId') ? $this->request->param('unitId/d') : 0;
		$appId = $this->request->param('appId') ? $this->request->param('appId/d') : 0;
            if (!$appId ) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
            }
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new AuthorityRequestDTO();
				$engprojectDo->filename['adminUserId']	= $adminUserId;
				$engprojectDo->filename['adminReg']	= $adminReg;
				$engprojectDo->filename['unitId']	= $unitId;
				$engprojectDo->filename['appId']	= $appId;

				$result = $api->navigationList($engprojectDo);


				if($result->code){
					if(empty($result->data)){
						$data = ['msg'=>$result->message,'code'=>'200','data'=>[]];
					} else {
					$node_tree = json_decode($result->data[0][0],true);

					$data = ['msg'=>$result->message,'code'=>'200','data'=>[["title"=>"管理后台","name"=>"System","path"=>"/", "redirect"=>"/home","component"=>"Main","children"=>array_values($node_tree)]]];
					}
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'异常','code'=>'500','data'=>[]];
			}
				return json_encode($data);
		}

	//删除管理员
	public function adminDelete()
    {
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId/d') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$userId = $this->request->param('userId') ? $this->request->param('userId') : 0;
            if (!$adminUserId  || !$userId) {
				$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
				return json_encode($data);
            }
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
				$api = new AuthorityServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new AuthorityRequestDTO();
				$engprojectDo->filename['adminUserId']	= $adminUserId;
				$engprojectDo->filename['adminReg']	= $adminReg;
				$engprojectDo->filename['userId']	= $userId;
				$result = $api->adminDelete($engprojectDo);

				if($result->code){
					$data = ['msg'=>$result->message,'code'=>'200','data'=>[]];
				} else {
					$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'异常','code'=>'500','data'=>[]];
			}
				return json_encode($data);
		}
	
/**********************************************************/
    
	
}
